package com.tdam2014.grupo02;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.net.ParseException;

import com.tdam2014.grupo02.Comment;
import com.tdam2014.grupo02.DataBase.DBHelper;

public class Picture {

private int pictureId;
private String pictureFlickrId;
private String pictureName;
private String pictureDescription;
private String picturePhoto;
private Date pictureDate;
private String folderName;
private int folderID;
private ArrayList<Comment> comments;
private static int dbVersion = 4;

public Picture (String name, String description, String photo, Date date, String folder, int folderID, String flickrId)
{
	this.pictureDescription = description;
	this.pictureName = name;
	this.picturePhoto = photo;
	this.setPictureDate(date);
	this.folderName = folder;
	this.folderID = folderID;
	this.pictureFlickrId = flickrId;
	//this.setComments(Comment.generateComment(pictureName));
	
}

public Picture (JSONObject jsonObj)
{
	this.pictureFlickrId = jsonObj.optString("id");
	this.pictureName = jsonObj.optString("title");
	this.pictureDescription = jsonObj.optString("title");
	//String tempDate = jsonObj.optString("pictureDate");
	//this.pictureDate = new Date(System.currentTimeMillis());
	
}


	public void setPictureDescription (String value)
	{
		this.pictureDescription = value;
	}
	public String getPictureDescription ()
	{
		return this.pictureDescription;
	}
	
	public void setPictureName (String value)
	{
		this.pictureName = value;
	}
	public String getPictureName ()
	{
		return this.pictureName;
	}
	
	public void setPicturePhoto (String value)
	{
		this.picturePhoto = value;
	}

	public String getPicturePhoto ()
	{
		return this.picturePhoto;
	}
	
	public Date getPictureDate() {
		return pictureDate;
	}
	public void setPictureDate(Date pictureDate) {
		this.pictureDate = pictureDate;
	}
	public void setPictureDate (JSONObject jsonObj) throws JSONException
	{
		JSONObject jsonPhoto = jsonObj.getJSONObject("photo");
		JSONObject jsonDate = jsonPhoto.getJSONObject("dates");
		String temp = jsonDate.optString("taken");
		Date value = null;
		try {
			value = ConvertToDate(temp);
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.pictureDate = value;
	}
	public static ArrayList<Picture> generatePictures(String folder, int folderId) 
	{
		ArrayList<Picture> list = new ArrayList<Picture>();
		
		for (int i = 0; i < 10; i++) {
			list.add(new Picture("Foto " + i, "Descripcion Foto " + i, "Carpeta " + i, new Date(System.currentTimeMillis()), folder, folderId, "" + i ));
		}
		return list;
	}
	public static ArrayList<Picture> generatePictures(int folderId, Context context) 
	{
		DBHelper dbHelper = new DBHelper(context, "FlickrDB", null, dbVersion);
		ArrayList<Picture> list = new ArrayList<Picture>();
		//Random ran = new Random();
		//int cantidad = ran.nextInt();
		for (int i = 0; i < 7; i++) 
		{
			Picture pic = new Picture("Foto " + i, "Descripcion Foto " + i, "Carpeta " + i, new Date(System.currentTimeMillis()), String.valueOf(folderId), folderId, "" + i );
			dbHelper.newPicture(pic, context);
		}
		return list;
	}
	
	
	public ArrayList<Comment> getComments() {
		
		return comments;
	}
	public void setComments(ArrayList<Comment> comments) {
		this.comments = comments;
	}
	public String getFolderName() {
		return folderName;
	}
	public void setFolderName(String folderName) {
		this.folderName = folderName;
	}
	public Picture isFolder (String folder)
	{
		
		if (folderName == folder)
		{
			return this;
		}
		else
		{
			return null;
		}
		
	}
	public static ArrayList<Picture> getPictures(Context context, int folder)
    {
        DBHelper dbHelper = new DBHelper(context, "FlickrDB", null, dbVersion);
    		
   	    return dbHelper.getPictures(context, folder);
    }
	public int getFolderID() {
		return folderID;
	}
	public void setFolderID(int folderID) {
		this.folderID = folderID;
	}
	public int getPictureId() {
		return pictureId;
	}
	public void setPictureId(int pictureId) {
		this.pictureId = pictureId;
	}
	public String getPictureFlickrId() {
		return pictureFlickrId;
	}
	public void setPictureFlickrId(String pictureFlickrId) {
		this.pictureFlickrId = pictureFlickrId;
	}
	public static Picture getPicture(Context context, String pictureId) {

		DBHelper dbHelper = new DBHelper(context, "FlickrDB", null, dbVersion);
		return dbHelper.getPicture(context, Integer.parseInt(pictureId));
	}
	
	private Date ConvertToDate(String dateString) throws java.text.ParseException
	{
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	    Date convertedDate = new Date();
	    try 
	    {
	        convertedDate = dateFormat.parse(dateString);
	    } 
	    catch (ParseException e) 
	    {
	        // TODO Auto-generated catch block
	        e.printStackTrace();
	    }
	    return convertedDate;
	}
	public static void updateAllPics (Context context, ArrayList<Folder> folders) throws ClientProtocolException, JSONException, IOException
    {
    	DBHelper dbHelper = new DBHelper(context, "FlickrDB", null, dbVersion);
    	ArrayList<Picture> pics = new ArrayList<Picture>();
    	for (int i=0; i<folders.size();i++)
    	{
    		pics.addAll(FlickrConnect.getPictures(folders.get(i).getFolderFlickrId(),folders.get(i).getFolderId()));
    	}
       	dbHelper.updatePictures(pics, context);   	
    }
	
	public static void updateFolderPics (Context context, Folder folder) throws ClientProtocolException, JSONException, IOException
    {
    	DBHelper dbHelper = new DBHelper(context, "FlickrDB", null, dbVersion);
    	ArrayList<Picture> pics = FlickrConnect.getPictures(folder.getFolderFlickrId(), folder.getFolderId());
       	dbHelper.updateFolderPictures(pics, context);   	
    }
	
}
